<?php
/**
 * 扩展由Grocery的上传图片压缩功能
 * zhaochengjiang
 * 
 */
class Thumbnails_ {
	// 配置
	var $config = array ();
	// 压缩前缀
	var $prefix = "s_";
	// 当前字段
	var $filed_name = "";
	// grocery
	var $grocery;
	/**
	 *
	 * @return the $config
	 */
	public function getConfig() {
		return $this->config;
	}
	/**
	 * @param multitype: $config        	
	 */
	public function setConfig($config) {
		$this->config = $config;
	}
	/**
	 *
	 * @return the $grocery
	 */
	public function getGrocery() {
		return $this->grocery;
	}
	
	/**
	 *
	 * @param field_type $grocery        	
	 */
	public function setGrocery($grocery) {
		$this->grocery = $grocery;
	}
	/**
	 * 缩略图回调函数
	 * 
	 * @param unknown_type $file_stream        	
	 * @param unknown_type $file_info        	
	 */
	public function thumbnail_callback($file_stream, $file_info) 
	{
		$config = $this->config[$file_info->field_name];
		$file_name = key($file_stream);
		if(is_array($file_name))
		{
			$file_name = $file_name [0];
		}
		$response_ = $this->thumbnail ( $file_name, $file_stream, $file_info->upload_path . '/', $config['w'],$config['w'], TRUE, TRUE);
		$object_rs = new stdClass ();
		if (empty( $response_ ['error'] )) {
			$rs = $response_ ['fileInfo'];
			$object_rs->name = $this->prefix . $rs ['file_name'];
			$object_rs->size = $rs ['file_size'];
			$object_rs->type = $rs ['file_type'];
			$object_rs->url = "/" . $file_info->upload_path . '/' . $object_rs->name;
			$object_rs->error = array ();
		} else {
			$object_rs->error = $response_ ['error'];
		}
		$upload_result [] = $object_rs;
		return $upload_result;
	}
	/**
	 * 开始上传
	 */
	public function start($crud,$thumbnail)
	{
		if(isset($thumbnail->config[$thumbnail->parseUrl()]))
		{
			$crud->callback_before_upload(array($thumbnail,"thumbnail_callback"));
		}
	}
	/**
	 * 
	 * @return Ambigous <>
	 */
	public function parseUrl()
	{
		$urlStr = $_SERVER["REQUEST_URI"];
		$arrUrl =explode("/",$urlStr);
		return $arrUrl[count($arrUrl) -1];
	}
	/**
	 * 开始缩略
	 * 
	 * @param unknown_type $name        	
	 * @param unknown_type $FILES        	
	 * @param unknown_type $uploadPath        	
	 * @param unknown_type $w        	
	 * @param unknown_type $h        	
	 * @param unknown_type $createThumb        	
	 * @param unknown_type $unlinkFile        	
	 * @return multitype:string |multitype:string Ambigous <string, unknown>
	 *         unknown |boolean
	 */
	protected function thumbnail($name, $FILES, $uploadPath, $w, $h, $createThumb = FALSE, $unlinkFile = FALSE) {
		$CI = &get_instance ();
		if (isset ( $FILES ) && ! empty ( $FILES )) {
			$error = '';
			$thumbName = '';
			$config = array ();
			$config = array (
					'upload_path' => $uploadPath,
					'allowed_types' => 'gif|jpg|png',
					'overwrite' => FALSE,
					'encrypt_name' => TRUE 
			);
			$CI->load->library ( 'upload', $config );
			$CI->upload->initialize ( $config );
			if ($CI->upload->do_upload ( $name )) {
				// 上传成功返回数组
				$data = $CI->upload->data ();
				// 缩图名称
				$thumbName = $createThumb ? $this->prefix . $data ['file_name'] : $data ['file_name'];
				
				if ($createThumb) {
					$config = array (
							'image_library' => 'gd2',
							'source_image' => $data ['full_path'],
							'maintain_ratio' => true,
							'width' => $w,
							'height' => $h,
							'new_image' => $thumbName 
					);
					$CI->load->library ( 'image_lib' );
					$CI->image_lib->initialize ( $config );
					$thumbInfo = '';
					if (! $thumbInfo = $CI->image_lib->resize ()) {
						$error = $CI->image_lib->display_errors ();
					}
					if (file_exists ( $data ['full_path'] ) && $unlinkFile) {
						unlink ( $data ['full_path'] );
					}
				}
			} else {
				$error = $CI->upload->display_errors ();
			}
			if ($error) {
				return array (
						'error' => $error 
				);
			}
			return array (
					'error' => '',
					'thumbName' => $thumbName,
					'fileInfo' => $data 
			);
		}
		return FALSE;
	}
}